package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.UUID;

@EnableConfigurationProperties(MinioProperties.class)
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private MinioProperties minioProperties;

    @Override
    public String upload(MultipartFile file) {
        System.out.println(minioProperties.getEndpoint());
        // 创建Minio客户端对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        //判断minio服务器中某个桶是否存在
        try {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder()
                    .bucket(minioProperties.getBucketName()).build());
            if (!found) {
                // 如果桶不存在就创建它
                minioClient.makeBucket(MakeBucketArgs.builder()
                        .bucket(minioProperties.getBucketName()).build());
            } else {
                System.out.println("Bucket " + minioProperties.getBucketName() + " already exists");
            }


            //获取上传的文件的名字
            String filename = file.getOriginalFilename();
            String suffix = filename.substring(filename.lastIndexOf("."));
            //使用UUID随机生成一个字符串作为上传到Minio服务器上的文件的名字
            String prefix = UUID.randomUUID().toString().replaceAll("-", "");
            //将当前日期格式化为yyyy/MM/dd的形式
            String format = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
            //拼接新的文件名
            String newFileName = format + "/"+prefix+suffix;
            //获取输入流
            InputStream is = file.getInputStream();
            //创建PutObjectArgs对象
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    //设置桶的名字
                    .bucket(minioProperties.getBucketName())
                    //设置上传到minio服务器之后的文件的名字
                    .object(newFileName)
                    .stream(is, is.available(), -1)
                    .build();
            // 通过客户端对象上传对象
            minioClient.putObject(putObjectArgs);
            //返回文件在minio服务器上的地址，例如：http://192.168.6.100:9000/test/ikun.gif
            return minioProperties.getEndpoint()+"/"+minioProperties.getBucketName()+"/"+newFileName;
        } catch (Exception e) {
            System.out.println("Error occurred: " + e);
        }
        return null;

    }
}
